<?php
	class blogAction extends userAction {
		private $feedSetting = false; //群组操作是否加入动态
		protected function _initialize() {
			parent::_initialize();
			$this->feedSetting = getAddFeedSetting($this->userInfo['id'],"blogaction");
		}
		
		public function index(){
			import("ORG.Util.Page"); //导入分页类	
			isset($_GET["u"]) ? $u = safeRequest($_GET["u"]) : $u= "all"; //条件参数
			isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;       //页数
			$blog = D("blogs");	
			if ("all"== $u){ //查找我与朋友所有的博客	
				$userId   	= 	$this->userInfo['id'];
				$friendIds 	= 	getFriendList($this->userInfo['id']);
				$blogCount 	= 	$blog->count("userid in ($friendIds) and showtype=1 and setting>0","id" ); 
				$listRows 	= 	C("blogListRows"); 
				$p 			= 	new Page($blogCount,$listRows);
				$p			->	setConfig('header' ,'篇博客 '); 
				$blogList	=	D("blogview")->findAll("blogs.userid in ($friendIds) and blogs.showtype=1 and blogs.setting>0 and blogs.userid=user.id","",'id desc',$p->firstRow.','.$p->listRows);
				$page  		= 	$p->show();
				$this		->	assign("blogList",$blogList);
				$this		->	assign("page",$page); 
				$this		->	display(); 
			} else {//根据用户ID查找相关用户的个人博客
				$this->rank =  getRank($this->userInfo['id'],$u,"blog");
				if(!$this->rank){
					redirect(__APP__."/rank/norank/u/".$u);
					return false;
				}
				$setting 	= 	getSetting($this->userInfo['id'],$u); //权限关系
				$blogCount 	= 	$blog->count("userid = $u and showtype=1 and setting >= $setting"); //获得该用户相关博客记录总数
				$listRows 	= 	C("blogListRows"); //从配置文件中读取博客每页显示记录数
				$p 			= 	new Page($blogCount,$listRows); //实例化分页类
				$p			->	setConfig('header' ,'篇博客 '); 
				$blogList	=	$blog->findAll("userid = '$u' and showtype=1 and setting >= $setting","*",'id desc',$p->firstRow.','.$p->listRows);//获得相关迷你博客记录
				$page  		= 	$p->show();
				if( $u != $this->userInfo['id']){
					$userDao = D("user");
					$userInfo = $userDao->find("id=".$u,"id,username");
					$this->assign("userInfo",$userInfo);
				}
				$this->assign("myId",$this->userInfo['id']);
				$this->assign("blogCount",$blogCount);
				$this->assign("blogList",$blogList); //绑定迷你博客数据
				$this->assign("page",$page); //绑定分页
				$this->display("my");
			}
		}
		
		//草稿箱
		public function drafty(){
			import("ORG.Util.Page"); //导入分页类
			$blog 		= 	D("blogs");
			$uid		= 	$this->userInfo['id'];
			$blogCount 	= 	$blog->count("userid = '$uid' and showtype=2"); //获得该用户相关博客记录总数
			$listRows 	= 	C("blogListRows"); //从配置文件中读取博客每页显示记录数
			$p 			= 	new Page($blogCount,$listRows); //实例化分页类
			$p			->	setConfig('header' ,L("page_title")); 
			$blogList	=	$blog->findAll("userid = '$uid' and showtype=2","*",'id desc',$p->firstRow.','.$p->listRows);//获得相关迷你博客记录
			$page  		= 	$p->show();
			$this		->	assign("blogCount",$blogCount); 
			$this		->	assign("blogList",$blogList); //绑定迷你博客数据
			$this		->	assign("page",$page); //绑定分页
			$this		->	display();
		}
		
		public function addblog(){
			$this->display();
		}
		
		public function saveblog(){
			if( "" == ( trim($_POST["title"]) ) || "" == ( trim($_POST["content"]) ) ){
				$this->error(L("no_input_full"));
				return false;
			}
			$blogDao 			= 	D("blogs");
			$blogDao->userid 	= 	$this->userInfo['id'];
			$blogDao->title 	= 	str_filter($_POST["title"]);
			$blogDao->content 	= 	str_filter($_POST["content"]);
			$blogDao->setting 	=   intval($_POST["privacy"]);			
			$blogDao->regtime 	= time();
			if(1 == $_POST["drafty"]){
				$blogDao->showtype = 2;
			}
			$id 				= $blogDao->add();
			if(	$id	>	0	){
				$userCache =  D("cache");
				$userCache -> setField("blogCount","(blogCount+1)","userid=".$this->userInfo['id']);
				if($this->feedSetting){
					addFeed($this->userInfo['id'],"blogs","发表了一篇新日志",$id);
				}
				$this->redirect('showblog','blog','',APP_NAME,array('id'=>$id),$delay=0,$msg='博客发表成功');
			}else{
				$this->error("博客发表失败");
			}
		}
		
		public function editblog(){
			$blogDao = D("blogs");
			$id = safeRequest($_GET["id"]);
			$blog = $blogDao->find("userid='".$this->userInfo['id']."' and id = ".$id);
			$this->assign("blog",$blog);
			$this->display();
		}
		
		public function updateblog(){
			$blogDao = D("blogs");
			$id = safeRequest($_POST["id"]);
			$blogDao->find("userid='".$this->userInfo['id']."' and id = ".$id);
			$blogDao->title = $_POST["title"];
			$blogDao->content = $_POST["content"];
			$blogDao->setting = $_POST["privacy"];
			if(1 == $_POST["drafty"]){
				$blogDao->showtype = 2;
			}else{
				$blogDao->showtype = 1;
			}
			if($blogDao->save()){
				//$this->success("博客编辑成功");
				$this->redirect('showblog','blog','',APP_NAME,array('id'=>$id),$delay=0);
			}else{
				$this->error(L("edit_error"));
			}	
		}
		
		public function utterblog(){
			$blogDao = D("blogs");
			$id = safeRequest($_GET["id"]);
			$blogDao->setField("showtype","1","userid=".$this->userInfo['id']." and id=".$id);
			$this->redirect('showblog','blog','',APP_NAME,array('id'=>$id),$delay=0);
		}
		
		public function deleteblog(){
			$blogDao = D("blogs");
			$id = safeRequest($_GET["id"]);
			if($blogDao->delete("userid='".$this->userInfo['id']."' and id = ".$id)){
				$userCache     =  D("cache");
				$userCache -> setField("blogCount","(blogCount-1)","userid=".$this->userInfo['id']);
				$blogRevertDao = D("blogs_reverts");		
				//delNotify(0,array("blogrevert","blogrevert2"),$id);
				delFeed(0,$this->userInfo['id'],"blogs",$id);
				$blogRevertDao->delete("blogid=".$id); //删除博客相关评论
				$this->redirect('index','blog','',APP_NAME,array('u'=>$this->userInfo['id']),0);
			}else{
				$this->error(L("delete_error"));
			}
			
		}
		
		public function showblog(){
			$id 		= 	safeRequest($_GET["id"]);
			$blogDao 	= 	D("blogs");
			$blog 		= 	$blogDao->find($id);
			if($blog){
				$bid 		= 	$blog['id'];
				$userid 	= 	$blog['userid'];
				
				if(!getRank($this->userInfo['id'],$userid,"other",$blog['setting'])){
					$this->error("由于隐私设置，不可见");
					return false;
				}
				
				if( $userid != $this->userInfo['id']){
					$userDao = D("user");
					$userInfo = $userDao->find($userid );
					$this->assign("userInfo",$userInfo);
					addHits("blogs",$bid); //增加点击率
					
				}
				//$blogRevertDao 	= 	D("blogs_reverts");
				$blogRevertDao 	= 	D("blogrevertview");
				$revertList 	= 	$blogRevertDao->findAll("blogs_reverts.blogid=".$bid." and blogs_reverts.userid=user.id");
				$this->assign("blog",$blog);
				$this->assign("myId",$this->userInfo['id']);
				$this->assign("revertlist",$revertList);
				$this->display();
			}else{
				$this->error(L("norecord"));
			}
		}
		
		public function getrevert(){
			$id 			= 	safeRequest($_GET["id"]);
			$blogRevertDao 	= 	D("blogs_reverts");
			$revertList 	= 	$blogRevertDao->findAll("blogid=".$id);
			return $revertList;
		}
		
		public function saverevert(){	
			$blogid = safeRequest($_POST["blogid"]);			
			$revertDao = D("blogs_reverts");
			$requestid = safeRequest($_POST["userid"]);
			$content = str_filter($_POST["content"]);
			$revertDao->blogid = $blogid;
			$revertDao->userid = $this->userInfo['id'];
			$revertDao->revertid = $requestid;
			$revertDao->content = $content;
			$revertDao->regtime = time();
			$id = $revertDao->add();
			if($id > 0){
				D("blogs")->setField("revertnum","(revertnum+1)","id=".$blogid);
				$reid = $_POST["reid"];
				if($requestid != $this->userInfo['id']){
					if($reid==0){
						addNotify($requestid,$this->userInfo['id'],"blogrevert",$blogid,$id);
					}else{
						addNotify($requestid,$this->userInfo['id'],"blogrevert2",$blogid,$id);
					}
				}
				if($reid != 0 && $reid != $requestid && $reid != $this->userInfo['id']){
					addNotify($reid,$this->userInfo['id'],"blogrevert2",$blogid,$id);
				}
				if($this->feedSetting){
					addFeed($this->userInfo['id'],"blogrevert",$content,$blogid,$id);
				}
				isset($_POST["ajax"]) ? print $id.",".$this->userInfo['id'].",".$this->userInfo['username'] : $this->success(L("revert_success"));
				//得返回josn数据
			}else{
				
				isset($_POST["ajax"]) ? print "error" : $this->error(L("revert_error"));;
			}
		}
	
		public function delrevert(){
			$revertid = safeRequest($_POST["revertid"]);
			$dao = D("blogs_reverts");
			$record = $dao->find("id=".$revertid,"blogid,userid,revertid,postid");
			if($record){
				if($record['userid'] == $this->userInfo['id'] || $record->revertsid == $this->userInfo['id']){
					$dao->delete("id=".$revertid);
					D("blogs")->setField("revertnum","(revertnum-1)","id=".$record['blogid']);
					if($record['postid'] == 0){
						delFeed(0,$this->userInfo['id'],"blogrevert",0,$revertid);
					}else{
						delFeed(0,$this->userInfo['id'],"blogrevert2",0,$revertid);
					}
					isset($_POST["ajax"]) ? print "success" : $this->success(L("delete_error"));
				}else {
					isset($_POST["ajax"]) ? print "error" : $this->success(L("delete_error"));
				}
			}else{
				isset($_POST["ajax"]) ? print "error" : $this->success(L("action_error"));
			}
		}
		
	}
		
?>